library(plyr)
library(magrittr)
library(tidyverse)
library(readxl)


rm(list=ls())
home = 'C:/Users/Jason/Dropbox/VNA_Responsiveness/Analysis/JOP-dataverse/'


permutation.results = paste0(home, 'RI-analyses.Rds') %>%
  readRDS

perm.cit.floor = subset(permutation.results, 
                        Result=='tab3col4x' & term=='Citizen')$estimate
perm.firm.floor = subset(permutation.results, 
                         Result=='tab3col4x' & term=='Firm')$estimate
perm.cf.diff.floor = perm.cit.floor - perm.firm.floor

perm.cit.query = subset(permutation.results, 
                        Result=='tab3col5x' & term=='Citizen')$estimate
perm.firm.query = subset(permutation.results, 
                         Result=='tab3col5x' & term=='Firm')$estimate
perm.cf.diff.query = perm.cit.query - perm.firm.query

perm.cit.caucus = subset(permutation.results, 
                         Result=='tab3col6x' & term=='Citizen')$estimate
perm.firm.caucus = subset(permutation.results, 
                          Result=='tab3col6x' & term=='Firm')$estimate
perm.cf.diff.caucus = perm.cit.caucus - perm.firm.caucus



experimental.results = paste0(home, 'experimental-analyses.Rds') %>%
  readRDS

exp.cit.floor = subset(experimental.results, 
                       Result=='tab3col4x' & term=='Citizen')$estimate
exp.firm.floor = subset(experimental.results, 
                        Result=='tab3col4x' & term=='Firm')$estimate
exp.cf.diff.floor = exp.cit.floor - exp.firm.floor

exp.cit.query = subset(experimental.results, 
                       Result=='tab3col5x' & term=='Citizen')$estimate
exp.firm.query = subset(experimental.results, 
                        Result=='tab3col5x' & term=='Firm')$estimate
exp.cf.diff.query = exp.cit.query - exp.firm.query

exp.cit.caucus = subset(experimental.results, 
                        Result=='tab3col6x' & term=='Citizen')$estimate
exp.firm.caucus = subset(experimental.results, 
                         Result=='tab3col6x' & term=='Firm')$estimate
exp.cf.diff.caucus = exp.cit.caucus - exp.firm.caucus



CI.cf.diff.floor = CI.cf.diff.query = CI.cf.diff.caucus = NULL
for(i in seq(from=-2, to=2, by=0.001)) {
  CI.cf.diff.floor = rbind(CI.cf.diff.floor, 
                           c(shift=i,
                             shifted=exp.cf.diff.floor+i,
                             upper=sum((exp.cf.diff.floor+i)>=perm.cf.diff.floor)/length(perm.cf.diff.floor),
                             lower=sum((exp.cf.diff.floor+i)<=perm.cf.diff.floor)/length(perm.cf.diff.floor)))
  CI.cf.diff.query = rbind(CI.cf.diff.query, 
                           c(shift=i,
                             shifted=exp.cf.diff.query+i,
                             upper=sum((exp.cf.diff.query+i)>=perm.cf.diff.query)/length(perm.cf.diff.query),
                             lower=sum((exp.cf.diff.query+i)<=perm.cf.diff.query)/length(perm.cf.diff.query)))
  CI.cf.diff.caucus = rbind(CI.cf.diff.caucus, 
                            c(shift=i,
                              shifted=exp.cf.diff.caucus+i,
                              upper=sum((exp.cf.diff.caucus+i)>=perm.cf.diff.caucus)/length(perm.cf.diff.caucus),
                              lower=sum((exp.cf.diff.caucus+i)<=perm.cf.diff.caucus)/length(perm.cf.diff.caucus)))
}; rm(i)
CI.cf.diff.floor = as.data.frame(CI.cf.diff.floor)
CI.cf.diff.query = as.data.frame(CI.cf.diff.query)
CI.cf.diff.caucus = as.data.frame(CI.cf.diff.caucus)



CIs.floor = c((-1)*CI.cf.diff.floor[CI.cf.diff.floor$lower>0.025,][sum(CI.cf.diff.floor$lower>0.025),]$shift, 
                exp.cf.diff.floor,
                (-1)*CI.cf.diff.floor[CI.cf.diff.floor$upper>0.025,][1,]$shift)
CIs.query = c((-1)*CI.cf.diff.query[CI.cf.diff.query$lower>0.025,][sum(CI.cf.diff.query$lower>0.025),]$shift, 
                exp.cf.diff.query,
                (-1)*CI.cf.diff.query[CI.cf.diff.query$upper>0.025,][1,]$shift)
CIs.caucus = c((-1)*CI.cf.diff.caucus[CI.cf.diff.caucus$lower>0.025,][sum(CI.cf.diff.caucus$lower>0.025),]$shift, 
              exp.cf.diff.caucus,
              (-1)*CI.cf.diff.caucus[CI.cf.diff.caucus$upper>0.025,][1,]$shift)



CIs = data.frame(rbind(CIs.floor,
                       CIs.query,
                       CIs.caucus)) %>%
  set_colnames(c('Lower',
                 'Estimate',
                 'Upper')) %>%
  mutate(Specification=factor(c('1) Floor debate; All delegates\nn = 470',
                                '2) Query session; All delegates\nn = 470',
                                '3) Group caucus; All delegates\nn = 470'), 
                              levels=c('1) Floor debate; All delegates\nn = 470',
                                       '2) Query session; All delegates\nn = 470',
                                       '3) Group caucus; All delegates\nn = 470')))
CIs

CIs.plot = ggplot(CIs, aes(x=Specification, y=Estimate, ymin=Lower, ymax=Upper)) + 
  geom_linerange(size=1) +
  geom_point(size=2) +
  geom_hline(yintercept=0, color='red', linetype=2) +
  scale_y_continuous(breaks=seq(-0.3, 0.3, 0.1), labels=round(seq(-0.3, 0.3, 0.1), 1)) +
  coord_cartesian(ylim=c(-0.274, 0.274)) +
  labs(x=NULL, y=expression(beta[Citizen]-beta[Firm])) +
  theme_minimal() +
  theme(panel.border=element_rect(color='black', fill=NA))
CIs.plot


ggsave(filename='appendix-figure-13-2.png', plot=CIs.plot, path=home, width=9, height=3, units='in')
ggsave(filename='appendix-figure-13-2.tiff', plot=CIs.plot, path=home, width=9, height=3, units='in')
